package com.kvn.jarslink;

import com.alipay.jarslink.api.ModuleConfig;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhiyuan.wang on 2018/4/4.
 */
public class ModuleScanner {
    private static String MODULE_PATH_PATTERN = "modules/*.jar";

    public static List<ModuleConfig> doScan() throws IOException {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources(MODULE_PATH_PATTERN);
        if (ArrayUtils.isEmpty(resources)) {
            throw new IllegalStateException("没有找到模块的jar");
        }

        List<ModuleConfig> list = new ArrayList<ModuleConfig>();
        for (Resource resource : resources) {
            ModuleConfig moduleConfig = new ModuleConfig();
            String jarName = resource.getFilename();
            String name_version = jarName.substring(0, jarName.length() - 4);
            int index = -1;
            if (name_version.toUpperCase().endsWith("SNAPSHOT")) {
                index = name_version.substring(0, name_version.length() - 9).lastIndexOf("-");
            } else {
                index = name_version.lastIndexOf("-");
            }
            String moduleName = name_version.substring(0, index);
            String moduleVersion = name_version.substring(index + 1);

            moduleConfig.setName(moduleName);
            moduleConfig.setEnabled(true);
            moduleConfig.setOverridePackages(ImmutableList.of("com.kvn." + moduleName)); // 可以突破双亲委派，同时加载一个jar的不同版本，做AB Test
            moduleConfig.setVersion(moduleVersion);
//            Map<String, Object> properties = new HashMap();
//            properties.put("url", "127.0.0.1");
//            moduleConfig.setProperties(properties);
            moduleConfig.setModuleUrl(ImmutableList.of(resource.getURL()));

            list.add(moduleConfig);
        }


        return list;
    }

    public static void main(String[] args) throws Exception {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("modules/*.jar");
        for (Resource resource : resources) {
            System.out.println(resource.getURL());
            String jarName = resource.getFilename();
            String name_version = jarName.substring(0, jarName.length() - 4);
            String moduleName = null;
            String moduleVersion = null;
            if (name_version.toUpperCase().endsWith("SNAPSHOT")) {
                int index = name_version.substring(0, name_version.length() - 9).lastIndexOf("-");
                moduleName = name_version.substring(0, index);
                moduleVersion = name_version.substring(index + 1);
                System.out.println("moduleName:" + moduleName + ", moduleVersion:" + moduleVersion);
            }
        }
    }

}
